close[x]


PHP

PHP-Home PHP-Environment Setup PHP-Syntax PHP-Run PHP in XAMPP PHP-Variable PHP-Comment PHP-Datatype PHP-String PHP-Operators PHP-Decision PHP-loop PHP-Get/Post PHP-Do While loop PHP-While loop PHP-For loop PHP-Foreach loop PHP-Array PHP-Multidimensional Arrays PHP-Associative Arrays PHP-Indexed Arrays PHP-Function PHP-Cookies. PHP-Session PHP-File upload PHP-Email PHP-Data & Time PHP-Include & Require PHP-Error PHP-File I/O PHP-Read File PHP-Write File PHP-Append & Delete File PHP-Filter PHP-Form Validation PHP-MySQl PHP-XML PHP-AJAX



learncodehere.com




PHP - Strings

  • PHP string is a sequence of characters.
  • There are two ways of creating strings in PHP:
  • Single-quote strings
  • Double-quote strings

  • Single-quote strings

    This type of strings does not processes special characters inside quotes.

    
     <?php 
    $x  = 'Code Always'; 
    echo $x; 
     ?>

    Single-quotes strings in PHP does not processes special characters.


    Double-quote strings

    Unlike single-quote strings, double-quote strings in PHP is capable of processing special characters.

    
      <?php 
    $x  = 'Code Always'; 
    echo $x; 
    echo "Welcome to $x"; 
     ?>  

    In PHP the character beginning with a backslash(“\”) are treated as escape sequences and are replaced with special characters. Here are few important escape sequences.

  • “\n” is replaced by a new line
  • “\t” is replaced by a tab space
  • “\$” is replaced by a dollar sign
  • “\r” is replaced by a carriage return
  • “\\” is replaced by a backslash
  • “\”” is replaced by a double quote
  • “\'” is replaced by a single quote

  • Example : Special Character

    
     <?php 
     $x  = 'Code Always.'; 
     echo $x; 
     echo "Welcome to $x \n"; 
     echo "$x "; 
     ?>

    Result : Special Character

    
     Welcome to Code Always.
     Code Always.   


    Built-In String Functions

    Built-in functions in PHP are some existing library functions which can be used directly in our programs making an appropriate call to them

    strlen() function

    The PHP strlen() function returns the length of a string.

    Example : strlen()

    
     <?php 
     echo strlen("Code Always."); 
     ?>

    The output of the above PHP code is '12'.

    strrev() function

    The PHP strrev() function reverses a string.

    Example : strrev()

    
     <?php 
     echo strrev("Code Always."); 
     ?> 

    The output of the above PHP code is '.syawlA edoC'.


    strpos() function

    The PHP strpos() function searches for a specific text within a string.

    Example : strpos()

    
     <?php 
     echo strpos("Code Always.","Always"); 
     ?>

    The output of the above PHP code is '6'.


    str_replace()() function

    The PHP str_replace() function replaces some characters with some other characters in a string.

    Example : str_replace()

    
     <?php 
     echo str_replace("Now","Always","Code Now."); 
     ?>

    The output of the above PHP code is 'Code Always.'.


    NOTE - Built-in string functions is given in function reference PHP String Functions